package com.login.service.user;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.login.bean.Buyer;
import com.login.dao.impl.BuyerDaoImpl;
import com.login.dao.impl.ManagerDaoImpl;
import com.login.dao.impl.SalerDaoImpl;

/**
 * Servlet implementation class UpdateUserServlet
 */
@WebServlet("/UpdateUserServlet")
public class UpdateUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
      
	BuyerDaoImpl updateBuyer=new BuyerDaoImpl();
	ManagerDaoImpl updateManager=new ManagerDaoImpl();
	SalerDaoImpl updateSeller=new SalerDaoImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateUserServlet() {
        super();
       
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		String type=request.getParameter("forgetPwd");
		System.out.println("更改密码的类型是"+type);
		String name=request.getParameter("updateName");
		String pwd=request.getParameter("resetPassword2");
		int res=0;
		if(type.equals("seller")){//更改卖家密码
			res=updateSeller.updateSaler(pwd, name);
			if(res>0){
				response.sendRedirect("login.jsp");
			}
			else{
				response.sendRedirect("error.jsp");
			}
		}else if(type.equals("buyer")){//更改买家密码
			res=updateBuyer.updateBuyer(pwd,name);
			if(res>0){
				response.sendRedirect("login.jsp");
			}
			else{
				response.sendRedirect("error.jsp");
			}
		}else{//更改管理员密码
			res=updateManager.updateManager(pwd, name);
			if(res>0){
				response.sendRedirect("login.jsp");
			}
			else{
				response.sendRedirect("error.jsp");
			}
		}	
		
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}



}
